package psico_test.appEjecutar.gui;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import psico_test.appEjecutar.appEjecutar;
import psico_test.utils.file.FileManager;

public class ventanaEjecutar extends JFrame implements Runnable, KeyListener
{
	private static final long serialVersionUID = 1L;

	private appEjecutar controller;
	public appEjecutar getController() {return controller;}
	public void setController(appEjecutar controller) {this.controller = controller;}
	
	public ventanaEjecutar(appEjecutar controller) 
	{
		this.controller = controller;
		this.addKeyListener(this);
	}

	public void run() 
	{
		initComponents();
		this.setTitle("Aplicación de Creación");
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void initComponents() 
	{
		jPanel1 = new javax.swing.JPanel();
		labelColor = new javax.swing.JPanel();
		labelTexto = new javax.swing.JLabel();
		BotonStop = new org.edisoncor.gui.button.ButtonIcon();
		BotonStart = new org.edisoncor.gui.button.ButtonIcon();
		Reloj = new org.edisoncor.gui.varios.ClockFace();
		jScrollPane1 = new javax.swing.JScrollPane();
		Lista = new javax.swing.JList();
		BotonBorrar = new javax.swing.JButton();
		BotonAñadir = new javax.swing.JButton();

		labelTexto.setText("");
		labelColor.add(labelTexto);
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, java.awt.Color.black, null), "Panel de pruebas:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), java.awt.Color.blue)); // NOI18N

		org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
				jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.add(labelColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
						.addContainerGap())
		);
		jPanel1Layout.setVerticalGroup(
				jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(jPanel1Layout.createSequentialGroup()
						.add(labelColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
						.addContainerGap())
		);

		BotonStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/stop.png"))); // NOI18N
		BotonStop.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				PararExperimento(evt);
			}
		});

		BotonStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/start.png"))); // NOI18N
		BotonStart.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				IniciarExperimento(evt);
			}
		});

		Reloj.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				ClickEnReloj(evt);
			}
		});

		org.jdesktop.layout.GroupLayout RelojLayout = new org.jdesktop.layout.GroupLayout(Reloj);
		Reloj.setLayout(RelojLayout);
		RelojLayout.setHorizontalGroup(
				RelojLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(0, 175, Short.MAX_VALUE)
		);
		RelojLayout.setVerticalGroup(
				RelojLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(0, 156, Short.MAX_VALUE)
		);

		Lista.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				ClickEnLista(evt);
			}
		});
		jScrollPane1.setViewportView(Lista);

		BotonBorrar.setText("Borrar");
		BotonBorrar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Borrar(evt);
			}
		});

		BotonAñadir.setText("Añadir");
		BotonAñadir.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				Añadir(evt);
			}
		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.add(18, 18, 18)
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
								.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
										.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
										.addContainerGap())
										.add(Reloj, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
												.add(BotonAñadir)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 50, Short.MAX_VALUE)
												.add(BotonBorrar)
												.addContainerGap())
												.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
														.add(BotonStart, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
														.add(BotonStop, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.addContainerGap())))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
								.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
										.addContainerGap()
										.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
										.add(layout.createSequentialGroup()
												.add(25, 25, 25)
												.add(Reloj, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 156, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
												.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
														.add(BotonAñadir)
														.add(BotonBorrar))
														.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 48, Short.MAX_VALUE)
														.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
																.add(BotonStop, 0, 0, Short.MAX_VALUE)
																.add(BotonStart, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, Short.MAX_VALUE))))
																.addContainerGap())
		);

		addWindowListener(new java.awt.event.WindowAdapter()
		{
			public void windowClosing(java.awt.event.WindowEvent evt)
			{
				Salir(evt);
			}
		});
		dlm = new DefaultListModel();
		this.Lista.setModel(dlm);
		this.BotonStart.setText("start");
		this.labelColor.setBackground(Color.gray);
		this.repaint();
		pack();
	}

	private void ClickEnReloj(java.awt.event.MouseEvent evt)
	{                              
		if(this.Reloj.isRomano())
			this.Reloj.setRomano(false);
		else
			this.Reloj.setRomano(true);
	}

	protected void Salir(WindowEvent evt) 
	{
		this.dispose();
		this.controller.salir();
	}

	private void IniciarExperimento(java.awt.event.ActionEvent evt) 
	{                                    
		if(!Lista.isSelectionEmpty())
			controller.iniciarExperimento(Lista.getSelectedValue());
		else
			JOptionPane.showMessageDialog(this,"Seleccione un experimento de la lista");
	}   
	
	private void PararExperimento(java.awt.event.ActionEvent evt)
	{                                  
		controller.pararExperimento();
	}                          

	private void Añadir(java.awt.event.ActionEvent evt) 
	{                        
		dlm.addElement(controller.añadirExperimento());
	}
	
	private void Borrar(java.awt.event.ActionEvent evt) 
	{                        
		Object aux = Lista.getSelectedValue();
		controller.borrarExperimento(aux);
		dlm.removeElement(aux);
	}                       

	private void ClickEnLista(java.awt.event.MouseEvent evt)
	{
		
	}
	
	//Componentes visuales de la ventana autogenerados
	private javax.swing.JButton BotonAñadir;
	private javax.swing.JButton BotonBorrar;
	private org.edisoncor.gui.button.ButtonIcon BotonStart;
	private org.edisoncor.gui.button.ButtonIcon BotonStop;
	private javax.swing.JList Lista;
	private org.edisoncor.gui.varios.ClockFace Reloj;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;

	private DefaultListModel dlm;
	public JPanel labelColor;
	public JLabel labelTexto;

	//ruta relativa de los iconos
	static final String IconoStart = "/resources/start.png";
	static final String IconoPause = "/resources/pause.png";
	static final String IconoStop = "/resources/stop.png";
	

	public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e){}
	
	public void keyPressed(KeyEvent e)
	{
		System.out.println("Pressed: Se ha pulsado una tecla: "+e.getKeyChar());	
	}
}
